﻿#region header

//  ***************************************************************************
// *                                LICENSE
//  ***************************************************************************
//  * "Haplous" .NET Simple 2D Game Framework
//  * 
//  * Copyright © Jonathan Evans 2014-2014
//  * 
//  * This program is free software; you can redistribute it and/or
//  * modify it under the terms of the GNU General Public License
//  * as published by the Free Software Foundation; either version 2
//  * of the License, or (at your option) any later version.
//  * 
//  * This program is distributed in the hope that it will be useful,
//  * but WITHOUT ANY WARRANTY; without even the implied warranty of
//  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  * GNU General Public License for more details.
//  * 
//  * You should have received a copy of the GNU General Public License
//  * along with this program; if not, write to the Free Software
//  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//  * 
//  ***************************************************************************
//  * 
//  * Contact: opencollar@googlemail.com
//  * 
//  * Website: http://haplous.codeplex.com/
//  *
//  * NuGet:   http://www.nuget.org/packages/haplous/
//  *
//  * File:    Haplous/Haplous/SynchronizedDirector.cs
//  *
//  ***************************************************************************

#endregion

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

using Haplous.Core;
using Haplous.Correctness;

using JetBrains.Annotations;

namespace Haplous
{
    /// <summary>
    ///     A controller that fires updates at regular intervals, based on a Cue's tick.
    /// </summary>
    [DebuggerDisplay("SynchronizedDirector: Name={Name}")]
    public abstract class SynchronizedDirector : Director
    {
        /// <summary>
        ///     The Cue used to trigger updates.
        /// </summary>
        [NotNull]
        private readonly Cue _cue;

        /// <summary>
        ///     Initializes a new instance of the <see cref="ElementBase" /> class.
        /// </summary>
        /// <param name="game">
        ///     The game to which the class belongs.  The <paramref name="game" /> must not be
        ///     <see langword="null" />.
        /// </param>
        /// <param name="name">
        ///     The unique name that can be used to identify this object.  The <paramref name="name" /> must not be
        ///     <see langword="null" />, empty or all whitespace.
        /// </param>
        /// <param name="actor">The Actor to control.  Must not be <see langword="null" />.</param>
        /// <param name="cue">The Cue that will be used to trigger movements.</param>
        protected SynchronizedDirector([NotNull] Game game, [NotNull] string name, [NotNull] Actor actor, [NotNull] Cue cue) : base(game, name, actor)
        {
            cue.ValidateArgument("cue");
            _cue = cue;
            cue.Tick += Tick;
        }

        /// <summary>
        ///     The Cue used to trigger updates.
        /// </summary>
        [NotNull]
        public Cue Cue
        {
            get { return _cue; }
        }

        /// <summary>
        ///     Called by the Cue when it is time to update.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        private void Tick([CanBeNull] object sender, [NotNull] EventArgs e)
        {
            try
            {
                if(!IsActive)
                    return;

                OnTick();
            }
            catch(Exception ex)
            {
                ErrorManager.OnUnhandledException(ex);
            }
        }

        /// <summary>
        ///     Called when an update is required.
        /// </summary>
        protected abstract void OnTick();
    }
}